{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "33d79755-d408-4092-a74d-0b2d9bc400e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from transformers import AutoTokenizer, AutoModelForCausalLM\n",
    "from sentence_transformers import SentenceTransformer\n",
    "import faiss\n",
    "import numpy as np\n",
    "import logging"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ef442456-fe4a-4f88-a2fc-e055fbd4a75a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set up logging to track any issues that might pop up\n",
    "logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "bc510aa1-36d4-4763-9b47-d1bef169f46e",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-08-06 11:34:53,883 - INFO - Use pytorch device_name: cpu\n",
      "2024-08-06 11:34:53,972 - INFO - Load pretrained SentenceTransformer: all-MiniLM-L6-v2\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5ae8707c29a44eb8872441ac8f0f914d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Batches:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "try:\n",
    "    # Load our language model and tokenizer\n",
    "    model_name = \"facebook/opt-1.3b\"\n",
    "    tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
    "    model = AutoModelForCausalLM.from_pretrained(model_name)\n",
    "\n",
    "    # Loading sentence transformer for embeddings\n",
    "    sentence_model = SentenceTransformer('all-MiniLM-L6-v2')\n",
    "\n",
    "    # Preparing knowledge base\n",
    "    knowledge_base = [\n",
    "    \"Category 1 -Login Issues -Login issues often occur due to incorrect passwords or account lockouts.\",\n",
    "    \"Category 2 -App Functionality -App crashes can be caused by outdated software or device incompatibility.\",\n",
    "    \"Category 3 -Billing -Billing discrepancies may result from processing errors or duplicate transactions.\",\n",
    "    \"Category 4 -Account Management -Account management includes tasks such as changing profile information, linking social media accounts, and managing privacy settings.\",\n",
    "    \"Category 5 -Performance Issues -Performance issues can be related to device specifications, network connectivity, or app optimization.\"\n",
    "]\n",
    "\n",
    "\n",
    "    # Create FAISS index for efficient retrieval\n",
    "    kb_embeddings = sentence_model.encode(knowledge_base)\n",
    "    index = faiss.IndexFlatL2(kb_embeddings.shape[1])\n",
    "    index.add(kb_embeddings)\n",
    "\n",
    "except Exception as e:\n",
    "    logging.error(f\"Error during initialization: {str(e)}\")\n",
    "    raise"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "803aa116-ee20-4467-befa-83116046fd9a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# This function finds the most relevant info from our knowledge base\n",
    "def retrieve_relevant_info(query, k=2):\n",
    "    try:\n",
    "        query_vector = sentence_model.encode([query])\n",
    "        _, I = index.search(query_vector, k)\n",
    "        return [knowledge_base[i] for i in I[0]]\n",
    "    except Exception as e:\n",
    "        logging.error(f\"Error in retrieve_relevant_info: {str(e)}\")\n",
    "        return []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "dae76c22-ea72-4572-acdb-5806c0498789",
   "metadata": {},
   "outputs": [],
   "source": [
    "def classify_ticket(ticket_text):\n",
    "    try:\n",
    "        # Retrieve relevant information\n",
    "        relevant_info = retrieve_relevant_info(ticket_text)\n",
    "        if not relevant_info:\n",
    "            return \"Classification unclear due to retrieval error\"\n",
    "        \n",
    "         # We're asking our model to classify the ticket based on the relevant info\n",
    "        prompt = f\"\"\"Classify the following support ticket into one of the given categories. \n",
    "Provide only the category number and name in your response, like 'Category X - Category Name'.\n",
    "\n",
    "Support Ticket: {ticket_text}\n",
    "\n",
    "Relevant Information:\n",
    "{relevant_info[0]}\n",
    "{relevant_info[1]}\n",
    "\n",
    "Only provide the category number and name in the format 'Category X - Category Name'.\n",
    "Classification:\"\"\"\n",
    "        \n",
    "        # Generate classification using LLM\n",
    "        inputs = tokenizer(prompt, return_tensors=\"pt\", max_length=512, truncation=True)\n",
    "        with torch.no_grad():\n",
    "            outputs = model.generate(**inputs, max_new_tokens=20, num_return_sequences=1, temperature=0.7)\n",
    "        classification = tokenizer.decode(outputs[0], skip_special_tokens=True)\n",
    "        \n",
    "        # Extract the category from the generated text\n",
    "        lines = classification.split('\\n')\n",
    "        for line in lines:\n",
    "            if line.startswith(\"Category\"):\n",
    "                return line.strip()\n",
    "        \n",
    "        return \"Classification unclear\"\n",
    "    except Exception as e:\n",
    "        logging.error(f\"Error in classify_ticket: {str(e)}\")\n",
    "        return \"Classification failed due to an error\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d788344c-afdb-40e1-a020-a02b84e860ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "support_tickets = [\n",
    "    {\"text\": \"My account login is not working. I've tried resetting my password twice.\"},\n",
    "    {\"text\": \"The app crashes every time I try to upload a photo.\"},\n",
    "    {\"text\": \"I was charged twice for my last subscription payment.\"},\n",
    "    {\"text\": \"I can't find the option to change my profile picture.\"},\n",
    "    {\"text\": \"The video playback is very laggy on my device.\"}\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "184ccf92-d3e0-4976-a415-ef519ddba348",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c8fd5b8d38934132858a54786c0bc78e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Batches:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ticket 1:\n",
      "Text: My account login is not working. I've tried resetting my password twice.\n",
      "Classification: Category 1 -Login Issues -Login issues often occur due to incorrect passwords or account lockouts.\n",
      "\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d67be632dca1417ebccb83c2487d7cb0",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Batches:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ticket 2:\n",
      "Text: The app crashes every time I try to upload a photo.\n",
      "Classification: Category 2 -App Functionality -App crashes can be caused by outdated software or device incompatibility.\n",
      "\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c6c822f46cf8481595c54bcc3e820848",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Batches:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ticket 3:\n",
      "Text: I was charged twice for my last subscription payment.\n",
      "Classification: Category 3 -Billing -Billing discrepancies may result from processing errors or duplicate transactions.\n",
      "\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ccf8d3b83cc2496980bd7cd101ae7348",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Batches:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ticket 4:\n",
      "Text: I can't find the option to change my profile picture.\n",
      "Classification: Category 4 -Account Management -Account management includes tasks such as changing profile information, linking social media accounts, and managing privacy settings.\n",
      "\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "71b11d60ac974e2994116d3d7322a78a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Batches:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ticket 5:\n",
      "Text: The video playback is very laggy on my device.\n",
      "Classification: Category 5 -Performance Issues -Performance issues can be related to device specifications, network connectivity, or app optimization.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Process all tickets\n",
    "for i, ticket in enumerate(support_tickets, 1):\n",
    "    try:\n",
    "        result = classify_ticket(ticket[\"text\"])\n",
    "        print(f\"Ticket {i}:\")\n",
    "        print(f\"Text: {ticket['text']}\")\n",
    "        print(f\"Classification: {result}\")\n",
    "        print()\n",
    "    except Exception as e:\n",
    "        logging.error(f\"Error processing ticket {i}: {str(e)}\")\n",
    "        print(f\"Ticket {i}: Error during processing\")\n",
    "        print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7dcc108d-e137-4bbc-a48e-b2918b49fd3a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
